﻿#include "JistCommonModel.h"
#include <QMessageBox>
void JistCommonData::copyData(const JistCommonData& data)
{
    m_bVisible = data.m_bVisible;
    m_Rect = data.m_Rect;
    m_Name = data.m_Name;
    m_Pos = data.m_Pos;
    m_img = data.m_img;
    m_Zlayer = data.m_Zlayer;
    m_iImgIndex = data.m_iImgIndex;
    m_iSpriteIndex = data.m_iSpriteIndex;
    m_UIOpt = data.m_UIOpt;
    m_iSubCnt = data.m_iSubCnt;
    m_AnchorPoint = data.m_AnchorPoint;

    if(data.m_pIItem){
        //m_pIItem = new ImageItem(&data.m_img);
        m_pIItem = new ImageItem(data.m_iImgIndex, data.m_iSpriteIndex, &data.m_pIItem->m_SpriteData, data.m_pIItem->m_oIndex_X, data.m_pIItem->m_oIndex_Y);
        m_pIItem->setZValue(data.m_pIItem->zValue());
        m_pIItem->setPos(data.m_pIItem->pos());
        m_pIItem->setOpacity(data.m_pIItem->opacity());
        m_pIItem->setRotation(data.m_pIItem->rotation());
        m_pIItem->setScale(data.m_pIItem->scale());
        m_pIItem->m_FlipH = data.m_pIItem->m_FlipH;
        m_pIItem->m_FlipV = data.m_pIItem->m_FlipV;
        m_pIItem->m_composition_mode = data.m_pIItem->m_composition_mode;
        m_pIItem->m_Image = data.m_pIItem->m_Image;
        m_pIItem->m_OriginPos = data.m_pIItem->m_OriginPos;
        m_pIItem->m_oIndex_X = data.m_pIItem->m_oIndex_X;
        m_pIItem->m_oIndex_Y = data.m_pIItem->m_oIndex_Y;
        m_pIItem->setScaleX(data.m_pIItem->m_ScaleXY.width());
        m_pIItem->setScaleY(data.m_pIItem->m_ScaleXY.height());
        m_pIItem->setVisible(data.m_pIItem->isVisible());
        m_pIItem->m_Id = data.m_pIItem->m_Id;
        if(data.m_pIItem->m_TextItem){
            //m_pIItem->m_TextItem = new QGraphicsTextItem( data.m_Name, m_pIItem, data.m_pIItem->scene() );
            m_pIItem->m_TextItem = new QGraphicsTextItem( data.m_Name, m_pIItem);
            m_pIItem->m_TextItem->setFont( data.m_pIItem->m_TextItem->font() );
            m_pIItem->m_TextItem->setPos(data.m_pIItem->m_TextItem->pos());
            m_pIItem->m_TextItem->setDefaultTextColor(QColor(255,255,255));
            m_pIItem->m_iAnchor = data.m_pIItem->m_iAnchor;
        }
    }
}




JistCommonModel::JistCommonModel(QObject *parent)
    :m_pList(NULL)
{
    m_pView = (JistTableView*)parent;

    connect(this, SIGNAL(add(int)), m_pView, SLOT(selectRow(int)));
}


void JistCommonModel::setViewWiget(JistTableView* view)
{
    m_pView = view;
}


QVariant JistCommonModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
/*
    if (index.row() >= listOfPairs.size() || index.row() < 0)
        return QVariant();

    if (role == Qt::DisplayRole) {
        QPair<QPixmap, QString> pair = listOfPairs.at(index.row());

        if (index.column() == 0)
            return pair.first;
        else if (index.column() == 1)
            return pair.second;
    }
    return QVariant();
*/

    //const Layer *layer = m_pList->llayerAt(layerIndex);
    JistCommonData p = (JistCommonData)m_pList->value(index.row());
    switch (role) {
        case Qt::DisplayRole:
            return p.m_Name;
        case Qt::EditRole:
            return QVariant();
        case Qt::UserRole + 4:
            return p.m_iSubCnt;
        case Qt::UserRole + 3:
            return p.m_UIOpt;
        case Qt::UserRole + 2:
            return p.m_img;
        case Qt::UserRole + 1:
            if(p.m_pIItem)
                return p.m_pIItem->isVisible();
            else
                return p.m_bVisible;//QVariant();
        case Qt::UserRole:
            return p.m_Rect;
        default:
            return QVariant();
    }
}


bool JistCommonModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid()) {
        int row = index.row();

        JistCommonData p = (JistCommonData)m_pList->value(row);

        switch (role) {
            case Qt::DisplayRole:
                p.m_Name = value.toString();
                break;
            case Qt::EditRole:
                break;
            case Qt::UserRole + 4:
                p.m_iSubCnt = value.toInt();
                break;
            case Qt::UserRole + 3:
                p.m_UIOpt = value.toInt();
                break;
            case Qt::UserRole + 2:
            {
                QVariant v;
                v.setValue(value);
                p.m_img = v.value<QImage>();
                //p.m_img = qVariantValue<QImage>(value);
                break;
            }
            case Qt::UserRole + 1:
                if(p.m_pIItem)
                    p.m_pIItem->setVisible(value.toBool());
                else
                    p.m_bVisible = value.toBool();
                break;
            case Qt::UserRole:
                p.m_Rect = value.toRect();
                break;
//            case Qt::UserType:
//                p = (JistCommonData)value;
//                break;
            default:
                return false;
        }

/*
        //if (index.column() == 0)
            p.first = value.toString();
        //else if (index.column() == 1)
            p.second = value.toString();
        else
            return false;
*/

        m_pList->replace(row, p);
        //m_pView->openPersistentEditor(index);
        //m_pView->closePersistentEditor(index);

        emit(dataChanged(index, index));

        return true;
    }
    return false;
}

int JistCommonModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    if(m_pList)
        return m_pList->size();

    else
        return 0;
}


bool JistCommonModel::insertRows(int position, int rows, const QModelIndex &index)
{
    Q_UNUSED(index);
    beginInsertRows(QModelIndex(), position, position+rows-1 );

    for (int row=0; row < rows; row++) {
        JistCommonData data;
        m_pList->insert(position, data);
    }

    endInsertRows();

    return true;
}


int JistCommonModel::IndexToRow(int Index) const
{
    return this->rowCount() - Index - 1;
}

// 데이터 이동시
void JistCommonModel::insertData(int index, JistCommonData *data)
{
    const int row = index;//m_pList->size() - index - 1 + 1;
    //데이터가 파괴되어 파괴되기전에 따로 지역변수를 이용함
    //JistCommonData data_ = *data;
    beginInsertRows(QModelIndex(), row, row);
    m_pList->insert(row, *data);
    endInsertRows();
//    emit layerAdded(index);
    const QModelIndex qIndex = this->index(row, 0, QModelIndex());
    //if(data->m_pIItem)
        m_pView->openPersistentEditor(qIndex);
}


JistCommonData JistCommonModel::takeAt(int index)
{
    //emit layerAboutToBeRemoved(index);
    const int row = index;//IndexToRow(index);
    beginRemoveRows(QModelIndex(), row, row);
    // 내부적으로 제거하는 연산 수행
    JistCommonData data = (JistCommonData)m_pList->takeAt(row);
    endRemoveRows();
    //emit layerRemoved(index);
    return data;
}


bool JistCommonModel::removeRows(int position, int rows, const QModelIndex &index)
{
    Q_UNUSED(index);
    beginRemoveRows(QModelIndex(), position, position+rows-1 );

    for (int row=0; row < rows; row++) {
        m_pList->removeAt(position);
    }
    int cnt = m_pList->count();
    int size = m_pList->size();
    endRemoveRows();

    return true;
}

void JistCommonModel::addData(JistCommonData data)
{
    //QList <JistCommonData>list = model->m_pList;
    //QPair<QPixmap, QString> pair(*gi, address);
    //QList <JistCommonData>tempDate;
    //tempDate.append(data);

    if (!this->m_pList->contains(data) || data.m_Rect.width() == 0)
    {
        insertRows(this->rowCount(), 1, QModelIndex());

        const QModelIndex index = this->index(this->rowCount()-1, 0, QModelIndex());
        if(data.m_pIItem)
            setData(index, data.m_pIItem->isVisible(), Qt::UserRole +1);
        else
            setData(index, data.m_bVisible, Qt::UserRole +1);
        //index = model->index(0, 1, QModelIndex());
        setData(index, data.m_Name, Qt::DisplayRole);
        setData(index, data.m_Rect, Qt::UserRole);
        setData(index, data.m_img, Qt::UserRole + 2);
        setData(index, data.m_UIOpt, Qt::UserRole + 3);
        setData(index, data.m_iSubCnt, Qt::UserRole + 4);
        //model->setData(index, data, Qt::UserRole);
        //removeTab(indexOf(newAddressTab));
        //if(data.m_pIItem)
            m_pView->openPersistentEditor(index);
    } else {
        QTextCodec *codec = QTextCodec::codecForName("UTF-8");
        QString localeStr = codec->toUnicode("같은 스프라이트가 이미 있습니다.");
        QMessageBox::information(m_pView, tr("Duplicate Sprite"),localeStr);
    }

    // 리스트의 모양 설정
    m_pView->refineViewNoUndo(this->rowCount()-1);
//    m_pView->selectRow(this->rowCount()-1);
//    m_pView->setFocus();
//    m_pView->resizeColumnsToContents();
//    m_pView->resizeRowsToContents();
    m_iRowCnt = this->rowCount();
}


//======================== 삭제 예정 ====================
void JistCommonModel::setImgFilePath(QString *imgFilePath)
{
    m_ImgFilePath = *imgFilePath;
}

QString JistCommonModel::getImgFilePath()
{
    return m_ImgFilePath;
}
//======================================================

int JistCommonModel::toModelIndex(const QModelIndex &index) const
{
    if (index.isValid()) {
        int rIdx = index.row();
        int idx = rowCount() - index.row() - 1;
        return idx;
    }
    else{
        return -1;
    }
}

void JistCommonModel::setDataList(QList< JistCommonData > *list)
{

    //if(m_pList->count())
    //    m_pList->clear();
    //this->reset();
    m_pList = list;

    int listCnt = list->count();
    for(int k = 0; k < listCnt; k++)
    {
        JistCommonData data = m_pList->value(k);
        const QModelIndex index = this->index(k, 0, QModelIndex());

        if(data.m_pIItem)
            setData(index, data.m_pIItem->isVisible(), Qt::UserRole +1);
        else
            setData(index, data.m_bVisible, Qt::UserRole +1);
        //index = model->index(0, 1, QModelIndex());
        setData(index, data.m_Name, Qt::DisplayRole);
        setData(index, data.m_Rect, Qt::UserRole);
        setData(index, data.m_img, Qt::UserRole + 2);
        setData(index, data.m_UIOpt, Qt::UserRole + 3);
        setData(index, data.m_iSubCnt, Qt::UserRole + 4);
        //model->setData(index, data, Qt::UserRole);
        //removeTab(indexOf(newAddressTab));
        //if(data.m_pIItem)
        m_pView->openPersistentEditor(index);
    }
}

void JistCommonModel::reset()
{
    beginResetModel();
    //QAbstractListModel::reset();
    //modelReset();
    endResetModel();
}
